package com.qf.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qf.entity.User;
import com.qf.mapper.UserMapper;
import com.qf.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.utils.Code;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2020-09-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public boolean register(User user) {
        String phone = user.getPhone();
        User result = userMapper.selectUserByPhone(phone);
        if (result == null) {
            userMapper.insert(user);
            return true;
        }
       return false;
    }

    @Override
    public boolean login(User user) {
        String phone = user.getPhone();
        String password = user.getPassword();
        User user1 = userMapper.selectUserByPhone(phone);
        String phone1 = user1.getPhone();
        String password1 = user1.getPassword();
        if (phone1.equals(phone) && password1.equals(password) ) {
            return true;
        }
        return false;
    }

    @Override
    public User get(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public boolean update(User user) {
        int i = userMapper.updateById(user);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkPhone(String phone) {

        User user = userMapper.selectUserByPhone(phone);
        if (user == null) {
            return true;
        }

        return false;
    }

    @Override
    public String sendCode(String phone) throws Exception{
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient需要的几个参数
        final String product = "Dysmsapi";//短信API产品名称（短信产品名固定，无需修改）
        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名（接口地址固定，无需修改）
        //替换成你的AK
        final String accessKeyId = "LTAI4G8MZfMMUvPaadd365PB";//你的accessKeyId
        final String accessKeySecret = "OVjjiIAAJqSJFB9Itr1KPCWaAsIsLw";//你的accessKeySecret
        //初始化ascClient,暂时不支持多region（请勿修改）
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        //要发生给的用户的电话号码
        request.putQueryParameter("PhoneNumbers", phone);
        //申请的短信签名
        request.putQueryParameter("SignName", "ABC商城");
        //申请的短信模板
        request.putQueryParameter("TemplateCode", "SMS_201471512");
        //给用户发送的验证码
        String code = Code.getMsgCode();
        Jedis jedis = new Jedis("",6379);
        jedis.setex(phone,300,code);

        request.putQueryParameter("TemplateParam", "{\"code\":\""+ code +"\"}");

        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        CommonResponse response = client.getCommonResponse(request);
        String data = response.getData();
        System.out.println(data);
        return data;
    }

    @Override
    public Integer getCode(String phone, String inputCode) {
        Jedis jedis = new Jedis("",6379);
        String code = jedis.get(phone);
        User user = userMapper.selectUserByPhone(phone);
        if (code.equals(inputCode)) {
            if (user == null) {
                user = new User();
                user.setPhone(phone);
                user.setPassword(code);
                user.setName("小贝帆");
                register(user);
            }
            user = userMapper.selectUserByPhone(phone);
            return user.getId();
        }
        return 0;
    }


}
